package com.example.hzq.food.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.hzq.food.R;
import com.example.hzq.food.bean.LoginBean;
import com.example.hzq.food.model.Model;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hzq on 2017/6/10.
 */

public class Login extends AppCompatActivity implements View.OnClickListener {
    private Button login;
    private Button regitister;
    private EditText username;
    private EditText userpas;
    public static String userid;
    private SharedPreferences sharedPreferences;
    private CheckBox checkBox;
    public  static  String uname;

    public static String getUname() {
        return uname;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        init();
        judge();
    }
    protected void judge(){
        if (sharedPreferences.getString("username","").equals("")){
            username.setText("");
            userpas.setText("");
            checkBox.setChecked(false);
        }
        else{
            username.setText(sharedPreferences.getString("username",""));
            userpas.setText(sharedPreferences.getString("userpassword",""));
            checkBox.setChecked(true);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        save();
        judge();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        save();
    }

    private void init(){
        username=(EditText)findViewById(R.id.editText);
        userpas=(EditText)findViewById(R.id.editText2);
        login=(Button)findViewById(R.id.button);
        regitister=(Button)findViewById(R.id.button2);
        login.setOnClickListener(this);
        regitister.setOnClickListener(this);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("check","flase");
        editor.commit();
    }
    public void save(){
        SharedPreferences.Editor editor=sharedPreferences.edit();
        if (checkBox.isChecked()){
            editor.putString("username",username.getText().toString());
            editor.putString("userpassword",userpas.getText().toString());
            editor.putString("check","true");
        }
        else {
            editor.putString("username","");
            editor.putString("userpassword","");
            editor.putString("check","flase");
        }
        editor.commit();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                if(username.getText().toString().equals("null")||userpas.getText().toString().equals("null")){
                    Toast.makeText(Login.this,"用户名或密码为空",Toast.LENGTH_SHORT).show();
                    return;
                }
               Model loginModel = new Model();
                Call<LoginBean> call=loginModel.login(username.getText().toString(), userpas.getText().toString());
                call.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            userid=response.body().getUserid();
                            if(userid.equals("0"))
                                Toast.makeText(Login.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                            else {
                               uname=Login.getUname();
                                Intent intent1=new Intent();
                                intent1.setClass(Login.this,MainActivity.class);
                                startActivity(intent1);
                            }
                        } else {
                            Toast.makeText(Login.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        Toast.makeText(Login.this,"登陆失败",Toast.LENGTH_SHORT).show();
                    }
                });
                save();
                break;
            case R.id.button2:
                save();
                Intent re=new Intent();
                re.setClass(Login.this,Register.class);
                startActivity(re);
                finish();
                break;
            default:break;
        }
    }
}
